package com.pdh.controller;

import com.pdh.bean.Express;
import com.pdh.bean.Message;
import com.pdh.bean.User;
import com.pdh.mvc.ResponseBody;
import com.pdh.service.ExpressService;
import com.pdh.service.UserService;
import com.pdh.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class ExpressController {
    /**
     * @param req
     * @param resp
     * @return String类型的[{size:总数,day:新增},{size:总数,day:新增}]
     */
    @ResponseBody("/express/consoleExpress.do")
    public String consoleExpress(HttpServletRequest req, HttpServletResponse resp) {
        Message msg = ExpressService.consoleExpress();
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    /**
     * 查询快递数据列表
     * @param req
     * @param resp
     * @return 返回String类型的json数据
     */
    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        //0.获取是否分页标志
        boolean paging = Boolean.parseBoolean(req.getParameter("paging"));
        //1.获取查询数据的起始索引值(偏移量)
        int offset = Integer.parseInt(req.getParameter("offset"));
        //2.获取当前页要查询的数量，注意不是当前页码 计算机不需要页码
        int limit = Integer.parseInt(req.getParameter("limit"));
        //3.查询数据
        Message msg = ExpressService.findAll(paging,offset,limit);
        /**
         *  String json = JSONUtil.toJSON(msg.getData()); 错误，不能被前端直接识别,BootStrapTable的处理风格
         *  它必须返回的ResultData的数据格式才能被前端成功接收到
         */
        String json = JSONUtil.toJSON(msg.getData());
        return json;
    }

    /**
     * @param req
     * @param resp
     * @return 返回插入结果
     */
    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) {
        //1.接收参数 四个参数
        String company = req.getParameter("company");
        String number = req.getParameter("number");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        // adminPhone参数是在管理员登陆的时候就存储在session里面的，直接获取即可
        String courierPhone = (String) ((Message) req.getSession().getAttribute("adminInfoMsg")).getData();

        //2.传递接收到的数据 其中的userPhone未知，创建一个userUtil类获取对应的号码 遵循对扩展开发对修改关闭
        Express express = new Express(number, userPhone, username, company,courierPhone);//这里录入的时候是没有录入人手机号码的
        Message msg = ExpressService.insert(express);
        if (msg.getStatus() == 0) {//录入成功
            //待处理：短信发送，这里设置位打印log到指定文件

            //查询user表中有无该用户(userPhone) 无则插入该用户
            Message MsgUser = UserService.findByUserPhone(userPhone);
            if (MsgUser.getStatus() != 0) {
                User newUser = new User(username, userPhone, null, null);
                UserService.insert(newUser);
            }
        } else {// status = -1
            //录入失败 单号重复导致录入失败
        }
        String json = JSONUtil.toJSON(msg);
        return json;//返回到客户端 其中的data为空
    }

    @ResponseBody("/express/findByNumber.do")
    //当传入的参撒不正确时也会出错HttpResponse resp会出现IllegalArgumentException: argument type mismatch
    public String findByNumber(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        Message msg = ExpressService.findByNumber(number);
        //转化为json格式并且return
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userPhone = req.getParameter("userPhone");
        int status = Integer.parseInt(req.getParameter("status"));
        Express newExpress = new Express(number,userPhone,username,company,status);

        Message msg = ExpressService.update(id, newExpress);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        Message msg = ExpressService.delete(id);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
